//
//  HRBulletinViewController.m
//  Economical report
//
//  Created by wyzc on 16/6/23.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//
#define CELLID @"CELL"

#import "HRBulletinViewController.h"
#import "HRBulletinModel.h"
#import "HRBulletinCell.h"

@interface HRBulletinViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 *  保存数据
 */
@property(nonatomic,strong)NSMutableArray *dataSourceArr;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
 *  标记是否选中
 */
@property(nonatomic,assign)BOOL isOpen;
/**
 *  标记选中的cell
 */
@property(nonatomic,assign)NSIndexPath *selectIndexPath;
@end

@implementation HRBulletinViewController
-(NSMutableArray *)dataSourceArr
{
    if (_dataSourceArr==nil) {
        _dataSourceArr=[NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.tableView.rowHeight=120;
    [self sendReueueDate];
    //注册xib
    UINib *nib=[UINib nibWithNibName:@"HRBulletinCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CELLID];
}
//请求网络数据
-(void)sendReueueDate
{
    NSURLSession *session=[NSURLSession sharedSession];
    NSURL *url=[NSURL URLWithString:@"http://api.21jingji.com/timestream/getList?page=1"];
    NSURLSessionDataTask *dataTask=[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *catName=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [catName writeToFile:@"/Users/wyzc/Desktop/kuaiBao.plist" atomically:YES];
        NSArray *arr=catName[@"list"];
        for (NSDictionary *dict in arr) {
            HRBulletinModel *model=[HRBulletinModel modelWithDict:dict];
            [self.dataSourceArr addObject:model];
        }
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self.tableView reloadData];
        }];
    }];
    [dataTask resume];
}
#pragma mark--tableView数据源代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRBulletinModel *model=self.dataSourceArr[indexPath.row];
    HRBulletinCell *cell=[tableView dequeueReusableCellWithIdentifier:CELLID];
    cell.model=model;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectIndexPath !=nil&&indexPath == self.selectIndexPath) {
        self.isOpen = !self.isOpen;
    }else{
        self.isOpen =YES;
    }
    self.selectIndexPath = indexPath;
    [self.tableView reloadData];
    }


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //HRBulletinModel *model=self.dataSourceArr[indexPath.row];
    
    if (_selectIndexPath != nil && _selectIndexPath == indexPath) {
        if (_isOpen) {
            return 200;
        } 
    } 
    return 120;
}
@end

